# -*- coding: utf-8 -*-
"""
Created on Mon Apr 27 15:54:00 2020

@author: Farman
"""

import pathlib
import sys


def _list_file_non_recursive(abs_path, pattern):
    '''
    List all files and directories in path 'abs_path'

    Parameters
    ----------
    path : string
        Full path of files and directories to be list.
        Non full path may cause undefined states or errors.
        
    pattern : string
        Pattern of files to list.

    Returns
    -------
        List of full path of dirs and files match the pattern 'pattern'.

    '''
    p = pathlib.Path(abs_path)
    fs = sorted(p.glob('*'))
    dirs  = []
    
    for f in fs:
        if f.is_dir():
            dirs.append(f)
    
    fs = sorted(p.glob(pattern))
    files = []
 
    for f in fs:
        if f.is_file():
          files.append(f) 
    
    return dirs, files

    
''' TEST CODE
#===================================================================

abs_path = r'/data/yule'
pattern = '*'

dirs, files = _list_file_non_recursive(abs_path, pattern)

print('\n=== Dirs ===\n')

for d in dirs:
    print(d)

print('\n=== Files ===\n')

for file in files:
    print(file)

print('')
#=============================================================='''



def list_files_non_recursive(abs_path, pattern):
    '''
    List all files in path 'abs_path' match pattern 'pattern'.

    Parameters
    ----------
    path : string
        Full path of files and directories to be list.
        Non full path may cause undefined states or errors.
        
    pattern : string
        Pattern of files to list.

    Returns
    -------
        List of full path of files match the pattern 'pattern'.

    '''
    dirs, files = _list_file_non_recursive(abs_path, pattern)
    return files


''' TEST CODE
#==============================================================
abs_path = r'/data/yule'
pattern = '*'

files = list_files_non_recursive(abs_path, pattern)

print('\n=== Files ===')

for file in files:
    print(file)
    
print('')
#=============================================================='''


def list_files_recursive(abs_path, pattern):
    '''
    List all files in path 'abs_path' match pattern 'pattern' recursively.

    Parameters
    ----------
    path : string
        Full path of files and directories to be list.
        Non full path may cause undefined states or errors.
        
    pattern : string
        Pattern of files to list.

    Returns
    -------
        List of full path of files match the pattern 'pattern'.

    '''
    dirs, files = _list_file_non_recursive(abs_path, pattern)
    
    for d in dirs:
        files += list_files_recursive(d, pattern)
    
    return files


''' TEST CODE
#==============================================================

abs_path = r'/data/yule'
pattern = '*.tif'

files = list_files_recursive(abs_path, pattern)

for file in files:
    print(file)
    
#=============================================================='''


if __name__ == '__main__':
    if len(sys.argv) != 4:
        print('')
        print('File List Tool')
        print('')
        print('Usage')
        print('=' * 30)
        print('    python listfiles.py [path_to_list] [pattern_of_file] [name_of_file_contains_result]')
        print('')
    else:
        path = sys.argv[1]
        pattern = sys.argv[2]
        file_name = sys.argv[3]
        files = list_files_recursive(path, pattern)
        files = [str(f) for f in files]

        with open(file_name, 'wt') as f:
            f.write('\n'.join(files))
